package org.hard.netty.c1;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * @author: 王林博
 * @date: 2022年08月11日 14:57
 */
public class TestByteBufferString {

    public static void main(String[] args) {
        // 字符串转换ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(16);
        byteBuffer.put("Hello".getBytes(StandardCharsets.UTF_8));

        // charset
        ByteBuffer he = StandardCharsets.UTF_8.encode("He");
        System.out.println((char) he.get(1));
        System.out.println((char) he.get(0));

        final ByteBuffer wrap = ByteBuffer.wrap("hel".getBytes(StandardCharsets.UTF_8));
        System.out.println((char) wrap.get(1));
        System.out.println((char) wrap.get(0));

        // 转为字符串
        final String s = StandardCharsets.UTF_8.decode(he).toString();
        System.out.println(s);

    }
}
